
package protocol.transactions;

import java.lang.reflect.Type;
import java.util.List;

import protocol.XoneProtocol;
import protocol.XoneServiceCode;
import protocol.meta.Source;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.netease.http.THttpRequest;

public class GetLatestSourceListTransaction extends XoneBaseTransaction {

    public GetLatestSourceListTransaction() {
        super(TRANSACTION_TYPE_GET_LATEST_SOURCE_LIST);
    }

    @Override
    public void onTransact() {
        THttpRequest request = XoneProtocol.getInstance().createGetLatestSourceListRequest();
        sendRequest(request);
    }

    @Override
    protected void onXoneTransactionSuccess(int code, Object obj) {
        if (obj instanceof JsonElement) {
            JsonElement list = ((JsonElement)obj).getAsJsonObject().get("list");
            List<Source> sourceList;
            Type collectionType = new TypeToken<List<Source>>(){}.getType();
            Gson gson = new Gson();
            sourceList = gson.fromJson(list, collectionType);
            notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, sourceList);
        }
    }

}
